import axios from 'axios'
import store from '@/store'
import freeApiData from '@/config/freeApiList'
import router from '@/router/index'
import {
  message
} from '@/libs/resetMessage';

import {
  getToken
} from '@/libs/util'
// import { Spin } from 'iview'
const addErrorLog = errorInfo => {
  const {
    statusText,
    status,
    request: {
      responseURL
    }
  } = errorInfo
  let info = {
    type: 'ajax',
    code: status,
    mes: statusText,
    url: responseURL
  }
 // if (!responseURL.includes('save_error_logger')) store.dispatch('addErrorLog', info)
}
class HttpRequest {
  constructor(baseUrl = baseURL) {
    this.baseUrl = baseUrl
    this.queue = {}
  }
  getInsideConfig() {
    const config = {
      baseURL: this.baseUrl,
      headers: {
        //
      }
    }
    return config
  }
  destroy(url) {
    delete this.queue[url]
    if (!Object.keys(this.queue).length) {
      // Spin.hide()
    }
  }

  interceptors(instance, url) {

    let _this = this;
    // 请求拦截
    instance.interceptors.request.use(config => {
      let token = getToken();
    // let token='2f2665c32753ebe0bf68057b79a79b71'
      let freeApi = []
      freeApi = freeApiData.freeApiData;
      if (freeApi.indexOf(config.url) < 0 && token) {
        config.headers.Authorization = token;
      } else {
        config.headers.Authorization = '';
        //return config;
      }
      //  config.headers.Authorization = '810d1ae1a6f3a36a2316aefe00b6dc02';
      // 添加全局的loading...
      if (!Object.keys(this.queue).length) {
        // Spin.show() // 不建议开启，因为界面不友好
      }
      this.queue[url] = true
      return config
    }, error => {
      return Promise.reject(error)
    })
    // 响应拦截
    instance.interceptors.response.use(res => {
     
      if (res.status == 200) {
        if (res.data.code === 0) {
                 } else if (res.data.code === 99999) {
          message({
            type: 'error',
            message: res.data.message || res.data.msg
          })
        } else {
          message({
            type: 'error',
            message: res.data.message || res.data.msg
          })
        }

      }
      this.destroy(url)
      const {
        data,
        status
      } = res
      return {
        data,
        status
      }


    }, error => {
      
      this.destroy(url)
      message({
        type: 'error',
        message:'网络出现异常'
      });
      let errorInfo = error.response
      if (!errorInfo) {
        const {
          request: {
            statusText,
            status
          },
          config
        } = JSON.parse(JSON.stringify(error))
        errorInfo = {
          statusText,
          status,
          request: {
            responseURL: config.url
          }
        }
      }
      addErrorLog(errorInfo)

      return Promise.reject(error)
    })
  }
  request(options) {
    const instance = axios.create()
    options = Object.assign(this.getInsideConfig(), options)
    this.interceptors(instance, options.url)
    return instance(options)
  }
}
export default HttpRequest
